#include "ODRoadAPI.h"
#include "FOpenDriveImporter.h"

using namespace ODRoadGeom;
using namespace std;

void    InitializeODRoadSystem(const wchar_t* filename, double UnitScale )
{
    TerminateODRoadSystem();
    ODSystem::Instance()->Name = std::wstring(filename);
    FOpenDriveImporter importer(filename, UnitScale, *ODSystem::Instance());

    //
    ODSystem::Instance()->GenerateVertices(); 
}

void    TerminateODRoadSystem()
{
    ODSystem::Instance()->Destroy();
}

// road
int32_t                 GetTotalRoadCount()
{
    return int32_t((ODSystem::Instance()->roadMap).size());
}

const char*             GetRoadId(int32_t index)
{
    int32_t i = 0;
    for (const auto& It : ODSystem::Instance()->roadMap)
    {
        if (i == index)
        {
            return It.first.c_str();
        }
        ++i;
    }
    return string().c_str();
}


void                    CollectRoadTriangles(const char* id, vector<glm::dvec3>& vertices, vector<glm::dvec3>& normals, vector<glm::dvec3>& tangent, vector<glm::dvec3>& binormal, vector<glm::dvec2>& uvs, vector<std::uint32_t>& indices, std::uint32_t& count, vector<string>& mats)
{
    ODRoadPtr road = ODSystem::Instance()->GetRoad(string(id));
    if (road != nullptr)
    {
        road->CollectTriangles(vertices, normals, tangent, binormal, uvs, indices, count, mats);
    }
}


void                    CollectRoadMarkTriangles(const char* id, vector<glm::dvec3>& vertices, vector<glm::dvec3>& normals, vector<glm::dvec3>& tangent, vector<glm::dvec3>& binormal, vector<glm::dvec2>& uvs, vector<std::uint32_t>& indices, std::uint32_t& count, vector<string>& mats)
{
    ODRoadPtr road = ODSystem::Instance()->GetRoad(string(id));
    if (road != nullptr)
    {
        road->CollectRoadMarkTriangles(vertices, normals, tangent, binormal, uvs, indices, count, mats);
    }
}

int32_t                 GetTotalJunctionCount()
{
    return (int32_t)((ODSystem::Instance()->junctionMap).size());
}
const char*             GetJunctionId(int32_t index)
{
    int32_t i = 0;
    for (const auto& It : ODSystem::Instance()->junctionMap)
    {
        if (i == index)
        {
            return It.first.c_str();
        }
        ++i;
    }
    return string().c_str();
}
void                    CollectJunctionTriangles(const char* id, vector<glm::dvec3>& vertices, vector<glm::dvec3>& normals, vector<glm::dvec3>& tangent, vector<glm::dvec3>& binormal, vector<glm::dvec2>& uvs, vector<std::uint32_t>& indices, std::uint32_t& count, vector<string>& mats)
{
    ODJunctionPtr jun = ODSystem::Instance()->GetJunction(string(id));
    if (jun != nullptr)
    {
        jun->CollectTriangles(vertices, normals, tangent, binormal, uvs, indices, count, mats);
    }
}



